package com.cloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.joyoung.common.service.impl.BaseServiceImpl;
import com.cloud.entity.ExceptionLogEntity;
import com.cloud.mapper.ExceptionLogMapper;
import com.cloud.req.QueryExceptionLogDto;
import com.cloud.service.ExceptionLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class ExceptionLogServiceImpl extends BaseServiceImpl<ExceptionLogMapper, ExceptionLogEntity> implements ExceptionLogService {

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;

    public List<ExceptionLogEntity> queryExceptionLog(QueryExceptionLogDto queryExceptionLogDto) {
        return exceptionLogMapper.queryExceptionLog(queryExceptionLogDto);
    }

    @Override
    public void updateExceptionLogById(ExceptionLogEntity entity) {
        exceptionLogMapper.updateExceptionLogById(entity);
    }

    @Override
    public void insertExceptionLog(Object object, String type, String tableName, String message) {

        try {
            //插入异常表
            ExceptionLogEntity exceptionLogEntity = new ExceptionLogEntity();
            exceptionLogEntity.setBody(JSON.toJSONString(object));
            exceptionLogEntity.setTableName(tableName);
            exceptionLogEntity.setType(type);
            exceptionLogEntity.setRetry(0);
            if (!StringUtils.isEmpty(message)) {
                exceptionLogEntity.setMessage(message);
            }
            this.insert(exceptionLogEntity);
        } catch (Exception e) {
            log.error("插入exception_log失败：" + e);
        }
    }
}
